import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
import { PrismaService } from 'src/middleware/prisma/prisma.service';
import { CreateInformDto } from './dto/create-inform.dto';
import { UpdateInformDto } from './dto/update-inform.dto';
import { InformVo } from './vo/inform-list.vo';

@Injectable()
export class InformService {
  @Inject(PrismaService)
  private prismaService: PrismaService;
  async create(createInformDto: CreateInformDto) {
    await this.prismaService.inform.create({
      data: createInformDto,
      select: {
        id: true,
        content: true,
        userId: true,
        createTime: true,
      },
    });
    return '新增成功';
  }

  async list(
    content: string,
    userId: string,
    status: string,
    pageNum: number,
    pageSize: number,
  ) {
    if (!pageNum) {
      pageNum = 1;
    }
    if (!pageSize) {
      pageSize = 10;
    }
    const inform = await this.prismaService.inform.findMany({
      skip: (Number(pageNum) - 1) * Number(pageSize),
      take: Number(pageSize),
      where: {
        isDetele: '1',
        // userId: Number(userId),
        status: status,
        content: {
          contains: content,
        },
      },
      select: {
        id: true,
        content: true,
        userId: true,
        status: true,
        type: true,
        createTime: true,
        updateTime: true,
      },
    });
    const res: InformVo[] = [];
    for (let i = 0; i < inform.length; i++) {
      const user = await this.prismaService.user.findUnique({
        where: {
          id: inform[i].userId,
        },
        select: {
          id: true,
          account: true,
          username: true,
          nickName: true,
          gender: true,
          avatarURL: true,
          email: true,
          phone: true,
          isAdmin: true,
          status: true,
          role: true,
          createTime: true,
          updateTime: true,
        },
      });
      res.push({
        ...inform[i],
        userInfo: user,
      });
    }
    const result = {
      list: res,
      pageNum: Number(pageNum),
      pageSize: Number(pageSize),
      total: inform.length,
    };
    return result;
  }

  async update(updateInformDto: UpdateInformDto) {
    try {
      await this.prismaService.inform.update({
        where: {
          isDetele: '1',
          id: updateInformDto.id,
        },
        data: {
          content: updateInformDto.content,
        },
        select: {
          id: true,
          content: true,
          userId: true,
          status: true,
          type: true,
          createTime: true,
          updateTime: true,
        },
      });
    } catch (error) {
      throw new HttpException('该消息不存在，修改失败', HttpStatus.BAD_REQUEST);
    }

    return '修改成功';
  }

  async romove(id: number) {
    // 数据库删除
    // return this.prismaService.inform.delete({
    //   where: {
    //     id,
    //   },
    // });
    // 业务逻辑删除（推荐）
    try {
      await this.prismaService.inform.update({
        where: {
          id: Number(id),
        },
        data: {
          isDetele: '0',
        },
        select: {
          id: true,
          content: true,
          userId: true,
          status: true,
          type: true,
          createTime: true,
          updateTime: true,
        },
      });
    } catch (error) {
      throw new HttpException('该消息不存在，删除失败', HttpStatus.BAD_REQUEST);
    }
    return '删除成功';
  }
}
